from app.extensions import db
from app.modules.common.utils import JsonEncoder


class LoginToken(db.Model, JsonEncoder):
    __tablename__ = "login_token"
    token_id = db.Column(db.String, primary_key = True)
    user_id = db.Column(db.String, nullable = False)
    user_name = db.Column(db.String, nullable = False)
    login_ip = db.Column(db.String)
    create_time = db.Column(db.DateTime)
    login_out_time = db.Column(db.DateTime)
    desc = db.Column(db.String)

    def __init__(self, token_id, user_id, user_name, create_time, login_out_time, login_ip = None, desc = None):
        self.token_id = token_id
        self.user_id = user_id
        self.user_name = user_name
        self.create_time = create_time
        self.login_out_time = login_out_time
        self.login_ip = login_ip
        self.desc = desc

    def to_dict(self):
        return {
            'token_id': self.token_id,
            'user_id': self.user_id,
            'user_name': self.user_name,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'login_out_time': self.login_out_time.strftime('%Y-%m-%d %H:%M:%S') if self.login_out_time else None,
            'desc': self.desc,
            'login_ip': self.login_ip,
        }
